import 'package:json_annotation/json_annotation.dart';
import 'package:logger/logger.dart';
import 'package:sqflite/sqflite.dart';

part 'LocalConversationRepository.g.dart';

class LocalConversationTypeConstants {
  static int general = 1;
  static int group = 2;
}

@JsonSerializable()
class LocalConversation {
  int? id;
  int? uid1;
  int? uid2;
  int? chatGroupId;
  int? type;
  int? updateTime;
  String? newestMsg;
  int? newestMsgType;
  int? unreadMsgCount;

  LocalConversation();

  LocalConversation.create(
      {this.id,
      this.uid1,
      this.uid2,
      this.chatGroupId,
      this.type,
      this.updateTime,
      this.newestMsg,
      this.newestMsgType,
      this.unreadMsgCount});

  static LocalConversation fromJson(Map<String, dynamic> json) {
    return _$LocalConversationFromJson(json);
  }

  Map<String, dynamic> toJson() {
    return _$LocalConversationToJson(this);
  }

  @override
  String toString() {
    return 'LocalConversation{id: $id, uid1: $uid1, uid2: $uid2, chatGroupId: $chatGroupId, type: $type, updateTime: $updateTime, newestMsg: $newestMsg, newestMsgType: $newestMsgType, unreadMsgCount: $unreadMsgCount}';
  }
}

class LocalConversationRepository {
  static const String _tableName = "local_conversation";

  static final Logger logger = Logger();

  static Future<void> addLocalConversation(
      Transaction transaction, LocalConversation conversation) async {
    transaction.insert(_tableName, conversation.toJson());
  }

  static Future<void> setNewestMsg(Transaction transaction, int? id,
      int updateTime, String newestMsg, int newestMsgType) async {
    // logger.i("setNewestMsg:$id, $updateTime, $newestMsg, $newestMsgType");
    await transaction.update(
      _tableName,
      {
        "updateTime": updateTime,
        "newestMsg": newestMsg,
        "newestMsgType": newestMsgType
      },
      where: "id = ?",
      whereArgs: [id],
    );
  }

  static Future<void> setUnReadMsgCount(
      Transaction transaction, Map<int, int> unreadMsgCountMap) async {
    Batch batch = transaction.batch();
    unreadMsgCountMap.forEach((conversationId, count) {
      batch.update(_tableName, {"unreadMsgCount": count},
          where: "id = ?", whereArgs: [conversationId]);
    });
    batch.commit();
  }

  static Future<LocalConversation?> getLocalConversationById(
      Transaction transaction, int id) async {
    List<Map<String, Object?>> dataList =
        await transaction.query(_tableName, where: "id = ?", whereArgs: [id]);

    if (dataList.isEmpty) {
      return null;
    }
    LocalConversation result = LocalConversation.fromJson(dataList[0]);
    return result;
  }

  static Future<List<LocalConversation>> listAllLocalConversation(
    Transaction transaction,
  ) async {
    List<Map<String, Object?>> dataList =
        await transaction.query(_tableName, orderBy: "updateTime desc");
    logger.i("dataList: $dataList");
    if (dataList.isEmpty) {
      return [];
    }
    return dataList.map((item) => LocalConversation.fromJson(item)).toList();
  }

  static Future<void> deleteLocalConversation(
      Transaction transaction, int id) async {
    await transaction.delete(_tableName, where: "id = ?", whereArgs: [id]);
  }
}
